<template lang="html">
    <el-dialog append-to-body :title="dialogObject.title" width="450px" :visible.sync="dialogObject.formVisible">
    <div style="padding-right:10px;">

    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item v-for="(searchField,index) in selectItemTagObj.searchFields" :label="searchField.label">
        <el-input v-model="searchForm[searchField.name]" :placeholder="searchField.label"></el-input>
        </el-form-item>
        <el-form-item>
        <el-button type="primary" @click="loadData" >查询</el-button>
        </el-form-item>
    </el-form>


    <el-table
        highlight-current-row
        :data="gridData.tableData"
        @row-click="rowClick"
        style="min-width:300px;height:220px;">
        <el-table-column  v-for="(gridField,index) in selectItemTagObj.gridFields" 
            :prop="gridField.name" 
            :label="gridField.label" 
            :show-overflow-tooltip="true">
        </el-table-column>
    </el-table>
    <div class="page_block" >
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="gridData.page.currentPage"
            :page-sizes="[5]"
            :page-size="gridData.page.pageSize"
            :pager-count="11"
            layout="prev, pager, next, jumper,total"
            :total="gridData.page.total" >
        </el-pagination>
    </div>
    <div style="text-align:right;padding-top:10px;" slot="footer" class="dialog-footer">
            <el-button @click="dialogObject.formVisible = false">取 消</el-button>
            <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
  </div>
</el-dialog>
</template>
<script>
export default {
    data() {
        return {
            selectItemTagObj:{
                searchFields:[],
                gridFields:[]
            },
            fieldName:null,
            dialogObject:{formVisible:false,title:'选择'},
            searchForm: {},
            gridData:{page:{currentPage:1,total:0, pageSize:5},tableData:[]},
            selectRow:null
        };
    },
    created() {
        var _this = this;
        // 加载字段信息
        
        
    },
    mounted() {
        var _this = this;
    },
    methods: {
        setTagData(selectItemTagObj, dialogObject, fieldName) {
            var _this = this;
            _this.selectRow =null;
            _this.selectItemTagObj = selectItemTagObj;
            _this.dialogObject = dialogObject;
            _this.fieldName = fieldName;
            _this.loadData();
        },
        /**
         * 获取列表数据
         */
        loadData() {
            var _this = this;
            _this.myUtils.appRequest({
                url: '/java' + _this.selectItemTagObj.url,
                method:'post',
                data:{pageNumber:_this.gridData.page.currentPage, pageSize:_this.gridData.page.pageSize, content:_this.searchForm},
                callback:function(data) {
                    _this.gridData.page.total = data.content.totalRow;
                    _this.gridData.page.currentPage = data.content.pageNumber;
                    _this.gridData.tableData = data.content.list;
                },
                catch: function(res) {
                    console.log(res);
                }
            }, _this);
        },
        /**
         * 改变每页大小
         */
        handleSizeChange(val) {
            this.gridData.page.pageSize = val;
            this.loadData();
        },
        /**
         * 改变页数
         */
        handleCurrentChange(val) {
            this.gridData.page.currentPage = val;
            this.loadData();
        },
        rowClick(row, event, column) {
            this.selectRow=row;
        },
        onSubmit() {
            var _this = this;
            if(_this.selectRow == null) {
                this.$message.error('请选择记录');
                return;
            }
            
            var id = _this.selectRow['id'];
            var name = _this.selectRow[_this.selectItemTagObj.gridFields[0].name];
            
            // _this.formData[_this.fieldName] = name;
            // var fieldId = _this.fieldName.substr(0,_this.fieldName.lastIndexOf('Name')) + 'Id';
            // _this.formData[fieldId] = id;
            // console.log(_this.formData);
            
            _this.$emit('update:time', new Date().toString());
            _this.$emit('selectItemResp', {id:id, name:name, fieldName:_this.fieldName});
            _this.dialogObject.formVisible = false;
            
        }
        
    },
    props:['time'],
};
</script>
<style>
.el-form-item__label {
  width:100px;
}
</style>
